<template>
  <div class="app-container">
    <el-card shadow="hover">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="margin-left: 20px;"
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="search"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          @click="reset"
        >
          重置
        </el-button>
      </div>
      <el-row :gutter="20" style="margin-top: 20px;">
        <el-form
          :model="queryParams"
          label-width="100px"
        >
          <el-col :span="6">
            <el-form-item label="课程名称:">
              <el-input
                v-model="queryParams.course"
                clearable
                @clear="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人姓名:">
              <el-input
                v-model="queryParams.name"
                clearable
                @clear="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话:">
              <el-input
                v-model="queryParams.phone"
                clearable
                @clear="search"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-card>

    <el-card shadow="hover" style="margin-top: 20px;">
      <div>
        <i class="el-icon-tickets"></i>
        <span> 优秀内容征集</span>

        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multipleList"
          @click="handleDeleteNews"
          style="margin-left: 20px;"
          >批量删除</el-button
        >
      </div>
      <el-table
        v-loading="loading1"
        :data="newsList"
        @selection-change="handleSelectionChange1"
        border
        style="margin-top: 10px;"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="课程名称"
          align="center"
          prop="course"
          show-overflow-tooltip
          min-width="150"
        ></el-table-column>
        <el-table-column
          label="课程资源介绍"
          align="center"
          prop="introduce"
          show-overflow-tooltip
          min-width="150"
        ></el-table-column>
        <el-table-column
          label="可获证书"
          align="center"
          prop="canBeCertificate"
          show-overflow-tooltip
          min-width="120"
        ></el-table-column>
        <el-table-column
          label="联系人姓名"
          align="center"
          prop="name"
          show-overflow-tooltip
          width="120"
        ></el-table-column>
        <el-table-column
          label="联系电话"
          align="center"
          prop="phone"
          show-overflow-tooltip
          width="150"
        ></el-table-column>
        <el-table-column
          label="工作单位"
          align="center"
          prop="workUnit"
          show-overflow-tooltip
          width="170"
        ></el-table-column>
        <!-- <el-table-column
          label="现居住地"
          align="center"
          prop="address"
          show-overflow-tooltip
          width="170"
        ></el-table-column> -->
        <el-table-column
          label="上传时间"
          align="center"
          prop="createTime"
          width="155"
        ></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="190">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDeleteNews(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="newsTotal > 0"
        :total="newsTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 查看 优秀内容征集内容 -->
    <el-dialog :title="title" :visible.sync="open" width="50%">
      <el-form ref="form" :model="form" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程名称:">
              <el-input v-model="form.course" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可获证书:">
              <el-input v-model="form.canBeCertificate" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="课程资源介绍:">
              <el-input
                type="textarea"
                autosize
                v-model="form.introduce"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人姓名:">
              <el-input v-model="form.name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话:">
              <el-input v-model="form.phone" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作单位:">
              <el-input v-model="form.workUnit" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="现居住地:">
              <!-- <el-input v-model="form.address" disabled></el-input> -->
              <el-cascader
                v-model="form.address"
                :options="options"
                disabled
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { queryGoodwork, delGoodwork,region } from "@/api/web/school";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";

export default {
  name: "Goodwork",
  components: {
    Editor
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading1: true,
      // 选中数组
      all: [],
      // 非单个禁用
      singleList: true,
      // 非多个禁用
      multipleList: true,
      newsTotal: 0,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        course: null,
        name: null,
        phone: null
      },
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      options: []
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    getOptions(){
      region().then(res => {
        this.options = res.data;
      })
    },
    search(){
      this.queryParams.pageNum = 1;
      this.getList();
    },
    reset(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        course: null,
        name: null,
        phone: null
      };
      this.getList();
    },
    getList() {
      this.loading1 = true;
      queryGoodwork(this.queryParams).then(res => {
        // console.log(res);
        this.newsList = res.rows;
        this.newsTotal = res.total;
        this.loading1 = false;
      });
    },
    handleView(row) {
      this.form = row;
      this.open = true;
      this.title = "查看优秀内容征集";
      this.form.address = JSON.parse(row.address);
    },
    handleSelectionChange1(selection) {
      this.all = selection.map(item => item.id);
      this.singleList = selection.length != 1;
      this.multipleList = !selection.length;
    },
    handleDeleteNews(row) {
      const id = row.id || this.all;
      console.log(id);
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delGoodwork(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>

<style scoped>
.select {
  margin-left: 20px;
  width: 220px;
}
</style>
